package com.example.myzhugezhaofang.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.activity.NewHouseActivity;
import com.example.myzhugezhaofang.adapter.NavigationAdapter;
import com.example.myzhugezhaofang.bean.NavigationBean;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NavigationFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NavigationFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";

    RecyclerView mRecyclerView;
    ArrayList<NavigationBean.ListBean> navigationList;
    private String mParam2;

    public NavigationFragment() {
    }

    public static NavigationFragment newInstance(ArrayList<NavigationBean.ListBean> navigationList) {
        NavigationFragment fragment = new NavigationFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, navigationList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            navigationList = (ArrayList<NavigationBean.ListBean>) getArguments().getSerializable(ARG_PARAM1);
        }

        Log.e("liangxq", "onCreate: " + navigationList.toString());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_navigation, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.rv_navigation);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 5);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        NavigationAdapter adapter = new NavigationAdapter(navigationList, getActivity(), R.layout.item_navigation);
        mRecyclerView.setAdapter(adapter);

        adapter.setItemListener(new NavigationAdapter.ItemListener() {
            @Override
            public void onItemClickListener(int pos) {
                if (pos == 0) {
                    Intent intent = new Intent(getActivity(), NewHouseActivity.class);
                    startActivity(intent);
                    Toast.makeText(getContext(), "新房页面", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}